<template>
  <div class="channel-edit">
    <!-- 单元格 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <!-- 编辑按钮 -->
      <van-button
        type="danger"
        plain
        round
        size="small"
        class="edit-btn"
        @click="isEdit = !isEdit"
        >{{ isEdit ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <!-- 宫格 -->
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(channel, index) in myChannels"
        :key="index"
        :text="channel.name"
        @click="onMyChannelClick(channel, index)"
      >
        <!--图标标签  -->
        <van-icon
          slot="icon"
          name="clear"
          v-show="isEdit && !fixedChannels.includes(channel.id)"
        ></van-icon>
        <!-- 文本样式插槽 -->
        <span class="text" slot="text" :class="{ active: index === active }">{{
          channel.name
        }}</span>
      </van-grid-item>
    </van-grid>
    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item
        icon="plus"
        class="grid-item"
        v-for="(channel, index) in recommendChannels"
        :key="index"
        :text="channel.name"
        @click="onAddChannel(channel)"
      />
    </van-grid>
  </div>
</template>

<script>
// 加载频道请求模块
import { getAllchannels, addUserChannel, deleteUserChannel } from '@/api/channel.js'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage.js'
export default {
  name: 'ChannelEdit',
  components: {

  },
  props: {
    // 频道数据
    myChannels: {
      type: Array,
      require: true
    },
    active: {
      type: Number,
      require: true
    }
  },
  data () {
    return {
      // 所有频道
      allChannels: [],
      // 控制编辑状态的显示与隐藏
      isEdit: false,
      fixedChannels: [0] // 不允许被删除的数据
    }
  },
  computed: {
    ...mapState(['user']),
    // 遍历我的已有频道 对比频道推荐 找到了即为已有频道 将未匹配到的元素追加到新数组中返回
    // recommendChannels () {
    //   const channels = [] // 遍历后满足条件的元素
    //   this.allChannels.forEach(item => {
    //     const res = this.myChannels.find(myChannels => {
    //       return myChannels.id === item.id
    //     })
    //     // 如果没有找到对应的 则加入频道推荐中
    //     if (!res) {
    //       channels.push(item)
    //     }
    //   })
    //   return channels
    // }
    recommendChannels () {
      return this.allChannels.filter(item => {
        return !this.myChannels.find(myChannels => {
          return myChannels.id === item.id
        })
      })
    }
  },
  created () {
    // 加载对应频道的列表数据
    this.loadAllChannels()
  },

  methods: {
    // 获取所有频道
    async loadAllChannels () {
      try {
        const { data } = await getAllchannels()
        this.allChannels = data.data.channels
        console.log(data)
      } catch (e) {
        this.$toast('获取数据失败')
      }
    },
    // 添加 频道推荐数据 到 我的频道中
    async onAddChannel (channel) {
      this.myChannels.push(channel)
      // 数据持久化操作
      if (this.user) {
        try {
          // 已登录 调用接口
          await addUserChannel({
            id: channel.id, // 频道ID
            seq: this.myChannels.length // 序号
          })
        } catch (err) {
          this.$toast('保存失败，请稍后重试')
        }
      } else {
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    },
    // 点击编辑按钮 执行删除操作
    onMyChannelClick (channel, index) {
      if (this.isEdit) {
        // 删除操作
        // 如果是固定频道 则不删除
        if (this.fixedChannels.includes(channel.id)) {
          return
        }
        // 删除指定项
        this.myChannels.splice(index, 1)

        // 更新激活的频道项
        if (index <= this.active) {
          this.$emit('update-active', this.active - 1, true)
        }
        // 调用 删除频道 方法
        this.deleteChannel(channel)
      } else {
        // 切换操作
        this.$emit('update-active', index, false)
      }
    },
    // 删除操作
    async deleteChannel (channel) {
      try {
        if (this.user) {
        // 已登录 调用接口删除数据
          await deleteUserChannel(channel.id)
        } else {
          // 未登录 更新本地存储数据
          setItem('TOUTIAO_CHANNELS', this.myChannels)
        }
      } catch (e) {
        this.$toast('操作失败，请稍后重试')
      }
    }
  }
}

</script>

<style lang="less" scoped>
.channel-edit {
    padding: 85px 0;
}
.title-text {
  font-size: 32px;
  color: #333;
}
.edit-btn {
  width: 104px;
  height: 48px;
  font-size: 26px;
  color: #f85959;
  border: 1px solid #f85959;
}
/deep/ .grid-item {
 width: 160px;
 height: 86px;
 .van-grid-item__content {
   white-space: nowrap; // 不折行
   background-color: #f4f5f6;
   .van-grid-item__text,.text {
     font-size: 28px;
     color: #222;
     margin-top: 0;
   }
   .active {
     color: red;
   }
   .van-grid-item__icon-wrapper {
     position: unset;
   }
 }
}
/deep/ .my-grid {
  .grid-item {
    .van-icon-clear {
      position: absolute;
      right: -10px;
      top: -10px;
      font-size: 30px;
      color: #cacaca;
      z-index: 2;
    }
  }
}
/deep/ .recommend-grid {
  .grid-item {
    .van-grid-item__content {
      flex-direction: row;
      .van-icon-plus {
        font-size: 28px;
        margin-right: 6px;
      }
    }
  }
}
</style>
